﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
using System.Runtime.InteropServices;

namespace CLFramework.Windows.Forms
{
    [ComVisible(true), PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public partial class WebBrowserForm : Form, IMessageFilter
    {
        // Fields
        private string startUrl = "http://blodfox777.cnblogs.com/";

        public string StartUrl
        {
            get { return startUrl; }
            set { startUrl = value; }
        }

        private const uint WM_KEYDOWN = 0x100;

        public WebBrowserForm()
        {
            this.InitializeComponent();
            base.WindowState = FormWindowState.Maximized;
            Application.AddMessageFilter(this);
            this.InitBrowser();
        }

        public WebBrowserForm(string url)
        {
            this.InitializeComponent();
            base.WindowState = FormWindowState.Maximized;
            Application.AddMessageFilter(this);
            StartUrl = url;
            this.InitBrowser();
        }

        public void ExitApp()
        {
            if (MessageBox.Show("确定要退出系统吗？", "请确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void InitBrowser()
        {
            this.mainBrowser.Url = new Uri(StartUrl);
            this.mainBrowser.AllowWebBrowserDrop = false;
            this.mainBrowser.ObjectForScripting = this;
        }

        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg != WM_KEYDOWN)
            {
                return false;
            }
            Keys keys = ((Keys)((int)m.WParam)) & Keys.KeyCode;
            if (keys == Keys.Escape)
            {
                this.ExitApp();
            }
            return true;
        }
    }
}
